package com.viettel.core.common.utils;

import java.util.Date;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class LogUtility {
	private static Logger responseLog = Logger
			.getLogger("com.viettel.touristone.cms.log.response");
	private static Logger serverErrorLog = Logger
			.getLogger("com.viettel.touristone.cms.log.error.server");
	private static Logger logTuning = Logger
			.getLogger("com.viettel.touristone.cms.log.tuning");

	public static void logResponse(String info, String methodName,
			String className) {
		responseLog.trace(className + "," + methodName + "," + info);
	}

	public static void logError(Throwable e, String message) {
		if(serverErrorLog.isEnabledFor(Level.ERROR)) {
			StringBuilder sb = new StringBuilder();
			sb.append("\"").append(toCSVCell(message)).append("\",");
			
			sb.append("\"");
			if(e != null) {
				sb.append(e.getMessage()).append("\n");
				StackTraceElement[] st = e.getStackTrace();
				for(int i = 0; i < st.length; i++) {
					sb.append(st[i]).append("\n");
				}
			}
			sb.append("\"");
			
			serverErrorLog.error(sb.toString());
		}
	}

	private static String toCSVCell(String input) {
		return input.replaceAll("\"", "\"\"");
	}
	
	public static void tunningLog(String service, String userId, String input,
			String output, Date beginTime, Date endTime, long executeTime) {
		if (logTuning.isEnabledFor(Level.INFO)) {
			StringBuilder sb = new StringBuilder();
			sb.append("\"").append(service).append("\",");
			sb.append("\"").append(userId).append("\",");
			sb.append("\"").append(toCSVCell(input)).append("\",");
			sb.append("\"").append(toCSVCell(output)).append("\",");

			sb.append("\"").append(beginTime).append("\",");
			sb.append("\"").append(endTime).append("\",");
			sb.append("\"").append(executeTime).append("\",");

			logTuning.info(sb.toString());
		}
	}
}
